// encrypt_test project main.go
package main

import (
	"crypto"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/base64"
	"fmt"
)

var appId string = "10651731"
var playerId string = "910086000114717546"
var ts string = "1483586023450"

var pubKey string = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmKLBMs2vXosqSR2rojMzioTRVt8oc1ox2uKjyZt6bHUK0u+OpantyFYwF3w1d0U3mCF6rGUnEADzXiX/2/RgLQDEXRD22er31ep3yevtL/r0qcO8GMDzy3RJexdLB6z20voNM551yhKhB18qyFesiPhcPKBQM5dnAOdZLSaLYHzQkQKANy9fYFJlLDo11I3AxefCBuoG+g7ilti5qgpbkm6rK2lLGWOeJMrF+Hu+cxd9H2y3cXWXxkwWM1OZZTgTq3Frlsv1fgkrByJotDpRe8SwkiVuRycR0AHsFfIsuZCFwZML16EGnHqm2jLJXMKIBgkZTzL8Z+201RmOheV4AQIDAQAB"
var sign string = "i1euIXPMz5SxfM9QRvY4VD5tKLxCRf1OjsnHfO78JJgPn6ywd+/ter5BJkZm8UmlFGJTNKQteRt1xNLQoTDaxGxIUs5GT5tnhP7qhaEPhYlgjS4imy8RK5AeUnY7pLA/c8PHcLVDWE/gUEqw7nbCY1k2OOfw4yeRKO2zUqv/Olyx9T92CjhIzuEJxq4cslN1nSmgCWb3kv0HjioZLoPESpUIMr1tiNLCUdT9zVYmOXBo94IuTceStJgsERgBGSL/rvOEb4l/HO72haW3XDgLIZQEKeDwwyya1D1Cyq+JrkpWp8p+B1w8zmTD+Yp25+4EosVziRNHMIVI+7sOQIm+Xw=="

func main() {
	message := fmt.Sprintf("%s%s%s", appId, ts, playerId)
	hash := sha256.New()
	hash.Write([]byte(message))
	hashed := hash.Sum(nil)

	signature, err := base64.StdEncoding.DecodeString(sign)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	keyBytes, err := base64.StdEncoding.DecodeString(pubKey)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	pubIf, err := x509.ParsePKIXPublicKey(keyBytes)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	pub := pubIf.(*rsa.PublicKey)

	err = rsa.VerifyPKCS1v15(pub, crypto.SHA256, hashed, signature)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println("verify signature success!")
}
